package com.fristvue.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fristvue.po.Goods;
import com.fristvue.po.Order;
import com.fristvue.po.Util.Page;
import com.fristvue.po.Util.ShopUtil;
import com.fristvue.service.GoodsService;
import com.fristvue.service.OrderService;

@RestController
public class OrderController {
	@Autowired
	private OrderService orderService;
	@Autowired
	private GoodsService goodsService;
	
	/**
	 * 分页查询
	 * @param page
	 * @param size
	 * @param shopUtil
	 * @return
	 */
	@RequestMapping("findOrder/{page}/{size}")
	public Page<Order> findOrder(@PathVariable("page") Integer page,
    		@PathVariable("size") Integer size, @RequestBody ShopUtil shopUtil){
		 Integer num = page * size;
		return this.orderService.listOrdersAll(num, size, shopUtil);
	}
	/**
	 * 添加订单
	 * @param order
	 * @return
	 */
	@PutMapping("addOrder")
	public boolean AddOrder(@RequestBody Order order) {
		Goods goods = this.goodsService.selectByPrimaryKey(order.getGoodsId());
		double price = goods.getPrice() * order.getOrderCount();
		order.setOrderPrice(price);
		return this.orderService.OrderAdd(order);
	}
	/**
	 * 删除订单
	 * @param id
	 * @return
	 */
	@DeleteMapping("deleteOrder")
	public boolean deleteOrder(Integer id) {
		return this.orderService.DeleteOrder(id);
	}
	/**
	 * 通过id查询订单的详细信息
	 * @param id
	 * @return
	 */
	@RequestMapping("OrderById")
	public Order OrderById(Integer id) {
		return this.orderService.orderById(id);
	}
	
	@PutMapping("updateOrderById")
	public boolean updateOrder(@RequestBody Order order) {
		return this.orderService.updateOrderById(order);
	}
}
